<template>
  <div class="login">
    <el-container>
      <!-- 点击图片跳转到首页 -->
      <el-link class="" @click="handleIndex" :underline="false"
        ><img
          style="height: 100px; width: 150px"
          src="../assets/miju2.jpg"
          alt=""
      /></el-link>

      <!-- 点击进入首页 -->
    </el-container>
    <div class="wrapper">
      <el-container>
        <el-form
          :model="formRegister"
          :rules="rules"
          ref="formRegister"
          class="demo-ruleForm"
        >
          <h1>用户注册</h1>

          <el-form-item label="账号" prop="name">
            <el-input
              placeholder="请输入需注册的帐号"
              v-model="formRegister.name"
            />
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="formRegister.password"
            />
          </el-form-item>

          <el-form-item label="确认密码" prop="checkPassword">
            <el-input
              type="password"
              placeholder="请确定密码"
              v-model="formRegister.checkPassword"
            />
          </el-form-item>

          <el-form-item label="手机号" prop="phone">
            <el-input placeholder="请输入手机号" v-model="formRegister.phone" />
          </el-form-item>

          <el-form-item prop="verificationCode" label="验证码">
            <el-input
              v-model="formRegister.verificationCode"
              placeholder="请填写验证码"
            >
              <template slot="append">
                <el-button
                  v-if="formRegister.showCode"
                  type="primary"
                  @click="getregisterPhoneCode"
                  >获取验证码</el-button
                >
                <div v-else>{{ formRegister.count }}</div>
              </template>
            </el-input>
          </el-form-item>

          <el-row>
            <el-button class="el-buttons" type="primary" plain @click="register"
              >注册</el-button
            >
          </el-row>
          <el-row>
            <el-link type="primary" @click="toLogin">前往登录</el-link>
            <el-link type="primary" @click="resetForm">重置</el-link>
          </el-row>
        </el-form>
      </el-container>
    </div>
    <el-footer>
      <a href="#" target="_blank">简体</a><span>|</span>
      <a href="#" target="_blank">繁体</a><span>|</span>
      <a href="#" target="_blank">English</a><span>|</span>
      <a href="#" target="_blank">常见问题</a><span>|</span>
      <a href="#" target="_blank ">隐私政策</a>
      <p class="copyright">
        Copyright ©2020 miju.futurefe.com All Rights Reserved.
      </p>
    </el-footer>
  </div>
</template>



<script>
export default {
  data() {
    // 验证手机号是否合法
    let checkMobile = (rules, value, cb) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[0123456789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;

      if (!value) {
        return cb(new Error("手机号不能为空!"));
      } else if (!regMobile.test(value)) {
        return cb(new Error("请输入正确的手机号"));
      } else {
        cb(); // 将判断传递给后面
      }
    };

    let checkUserName = (rule, value, cb) => {
      var reg = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/; //正则验证账号
      if (!value) {
        return cb(new Error("账号不能为空!"));
      } else if (!reg.test(value)) {
        return cb(new Error("字母开头,长度5-16之间,允许字母数字下划线"));
      } else {
        cb(); // 将判断传递给后面
      }
    };
    let checkPassword = (rule, value, cb) => {
      if (!value) {
        return cb(new Error("密码不能为空!"));
      } else if (value.length < 6) {
        return cb(new Error("密码长度最小6位"));
      } else {
        cb();
      }
    };
    let checkPasswordAgain = (rule, value, cb) => {
      if (!value) {
        return cb(new Error("再次输入密码不能为空!"));
      } else if (value !== this.formRegister.password) {
        return cb(new Error("两次输入密码不一致!"));
      } else {
        cb();
      }
    };

    return {
      rules: {
        name: [
          {
            validator: checkUserName,
            trigger: "blur",
            required: true,
          },
        ],
        password: [
          {
            validator: checkPassword,
            trigger: "blur",
            required: true,
          },
        ],
        checkPassword: [
          {
            validator: checkPasswordAgain,
            trigger: "blur",
            required: true,
          },
        ],
        phone: [
          {
            validator: checkMobile,
            trigger: "blur",
            required: true,
          },
        ],
        verificationCode: [
          {
            required: true,
            message: "请输入手机验证码",
            trigger: "blur",
          },
        ],
      },
      formRegister: {
        name: "",
        password: "",
        checkPassword: "",
        phone: "",
        verificationCode: "", //表单中展示的验证码
        timer: null,
        showCode: true, //判断展示‘获取验证码’或‘倒计时’
        count: "", //倒计时时间
      },
    };
  },
  methods: {
    // 获取手机验证码
    getregisterPhoneCode() {
      // 如果未输入手机号，结束执行
      if (
        this.formRegister.phone == "" &&
        !this.regMobile.test(this.formRegister.phone)
      ) {
        return;
      }

      let phoneCode = {
        phone: this.formRegister.phone,
      };
      // 调用接口，向手机号发送验证码
      this.$axios.post("users/auth", phoneCode).then((res) => {
        console.log(res);
        if (res.status != 200) {
          return this.$message.error("验证码发送失败！");
        } else {
          // 当验证码发送成功，开始60秒倒计时
          const TIME_COUNT = 60;
          if (!this.formRegister.timer) {
            this.formRegister.showCode = false;
            this.formRegister.count = TIME_COUNT;
            this.formRegister.timer = setInterval(() => {
              if (
                this.formRegister.count > 0 &&
                this.formRegister.count <= TIME_COUNT
              ) {
                this.formRegister.count -= 1;
              } else {
                this.formRegister.showCode = true;
                clearInterval(this.formRegister.timer);
                this.formRegister.timer = null;
              }
            }, 1000);
          }
        }
      });
    },

    resetForm() {
      this.$refs["formRegister"].resetFields(); //重置表单
    },
    toLogin() {
      this.$router.push({
        path: "/Login", //点击跳转到登录界面
      });
    },

    register() {
      this.$refs.formRegister.validate((valid) => {
        if (valid) {
          this.$axios
            .post("register", {
              username: this.formRegister.name,
              password: this.formRegister.password,
              phone: this.formRegister.phone,
              code: this.formRegister.verificationCode,
            })
            .then((response) => {
              if (response.data.meta.status === 201) {
                this.$router.push("/Login"); //注册成功所跳转的页面
                console.log(response);
                this.$message({
                  message: "注册成功",
                  type: "success",
                });
              } else {
                this.$message({
                  message: response.data.meta.msg,
                  type: "error",
                });
                console.log(response);
                return false;
              }
            })
            .catch(function (error) {
              console.log(error);
            });
        } else {
          this.$message({
            message: "账号或密码、手机格式不正确，请重新填写",
            type: "error",
          });
          return false;
        }
      });
    },
    handleIndex() {
      this.recordNum = "1";
      this.$emit("index", "1");
      this.$router.push("Index");
    },
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}

body {
  font-family: Helvetica Neue, Helvetica, Arial, Microsoft Yahei,
    Hiragino Sans GB, Heiti SC, WenQuanYi Micro Hei, sans-serif;
}

.login {
  color: #333333;
  font-size: 12px;
  margin: 0;
}

.el-container {
  height: 98px;
  color: #333333;
  font-size: 12px;
  margin: 0;
  position: relative;
  width: 1226px;
  margin-right: auto;
  margin-left: auto;
}

.el-container img {
  width: auto;
  height: 100%;
}

.wrapper {
  background: url("../assets/bg.jpg") no-repeat center;
}

.wrapper .el-container {
  height: 660px;

  color: #333333;
  font-size: 12px;
  margin: 0;
  padding: 0;
  outline: none;
  position: relative;
  width: 1226px;
  margin-right: auto;
  margin-left: auto;
}

.wrapper .el-container .demo-ruleForm {
  color: #333333;
  font-size: 12px;
  box-sizing: border-box;
  width: 410px;
  height: 610px;
  background-color: #ffffff;
  position: absolute;
  bottom: 30px;
  right: 0;
  overflow: hidden;
  padding: 0 31px;
  border-radius: 10px;
}

.demo-ruleForm h1 {
  text-align: center;
  margin-top: 20px;
}

.wrapper .el-container .demo-ruleForm .el-form-item {
  margin-bottom: 10px;
}

.wrapper .el-container .demo-ruleForm .el-buttons {
  width: 86%;
  line-height: 50px;
  margin-top: 10px;
  font-size: 16px;
  margin-left: 20px;
  padding: 0px;
}

.wrapper .el-container .demo-ruleForm .el-link {
  padding: 10px 50px 10px 60px;
}

.wrapper .el-container .demo-ruleForm .tips .sms {
  color: #ff6600;
}

.wrapper .el-container .demo-ruleForm .tips .reg {
  color: #999999;
}

.wrapper .el-container .demo-ruleForm .tips .reg span {
  margin: 0 7px;
}

.wrapper .el-container .demo-ruleForm .el-container img {
  width: 160px;
  height: 160px;
  background-color: #ccc;
  margin: 80px 80px;
}

.wrapper .el-container .demo-ruleForm .el-container .beizhu {
  text-align: center;
  font-size: 13px;
  margin-top: 10px;
  color: #999;
}

.el-footer {
  height: 100px;
  padding-top: 60px;
  color: #999999;
  font-size: 16px;
  text-align: center;
}

.el-footer a {
  color: #999999;
  display: inline-block;
  text-decoration: none;
}

.el-footer .span {
  margin: 0 10px;
}

.el-footer .copyright {
  margin-top: 13px;
}
</style>
